﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace _395_2d
{
    class CollisionDetection
    {
        private Ship Player;
        private IList playerProjectiles;

        private IList enemies;
        private IList enemyProjectiles;

        public CollisionDetection(Player p, IList pp, IList e, IList ep)
        {
            Player = p;
            playerProjectiles = pp;
            enemies = e;
            enemyProjectiles = ep;

            intersect();
        }

        public void intersect()
        {
            for (int i = enemyProjectiles.Count - 1; i >= 0; i--)
            {
                Projectile p = (Projectile)enemyProjectiles[i];

                if ( Player.intersects(p) )
                {
                    Player.hit(p);
                    enemyProjectiles.RemoveAt(i);
                }
            }

            for (int i = enemies.Count - 1; i >= 0; i--) 
            {
                Ship s = (Ship)enemies[i];

                for (int j = playerProjectiles.Count - 1; j >= 0; j--)
                {
                    Projectile p = (Projectile)playerProjectiles[j];

                    if (s.intersects(p))
                    {
                        s.hit(p);
                        playerProjectiles.RemoveAt(j);
                    }
                }

                if (s.Health <= 0)
                {
                    enemies.RemoveAt(i);
                }
            }

            foreach (Sprite s in enemies)
            {
                if (s.intersects(Player))
                {
                    Player.Health = 0;
                }
            }
        }

        public void Clear()
        {
            Player = null;
            playerProjectiles.Clear();
            enemies.Clear();
            enemyProjectiles.Clear();
        }
    }
}